﻿using System.Data;
using System.Text;

namespace NazTek.Utility.Extensions.IO
{
    /// <summary>
    /// Extends DataTable
    /// </summary>
    public static class DataTableExtension
    {
        /// <summary>
        /// Gets plain xml text represenation of the DataTable
        /// </summary>
        /// <param name="source">DataTable to serialize</param>
        /// <returns>XML string representation of the DataTable</returns>
        /// <example>
        /// <code>
        /// // &lt;DataSet&gt;
        /// //   &lt;DataTableName&gt;
        /// //     &lt;Column1&gt;Bob&lt;/Column1&gt;
        /// //     &lt;Column2&gt;1950-04-19T00:00:00-05:00&lt;/Column2&gt;
        /// //   &lt;/DataTableName&gt;
        /// //   &lt;DataTableName&gt;
        /// //     &lt;Column1&gt;Jane&lt;/Column1&gt;
        /// //     &lt;Column2&gt;1990-01-01T00:00:00-06:00&lt;/Column2&gt;
        /// //   &lt;/DataTableName&gt;
        /// // &lt;/DataSet&gt;
        /// MyDataTable.GetUnformattedXml();
        /// </code>
        /// </example>
        public static string GetUnformattedXml(this DataTable source)
        {
            using (DataSet ds = new DataSet("DataSet"))
            {
                ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
                ds.Tables.Add(source);
                return ds.GetUnformattedXml();
            }
        }
        /// <summary>
        /// Gets delimited text representation of the DataTable
        /// </summary>
        /// <param name="source">The data source</param>
        /// <param name="delimiter">Field value separator</param>
        /// <param name="textFieldPrefix">Prefix text fields with</param>
        /// <param name="textFieldPostfix">Postfix text fields with</param>
        /// <returns>Delimited text</returns>
        public static string GetDelimitedText(this DataTable source, string delimiter, string textFieldPrefix = "", string textFieldPostfix = "")
        {
            StringBuilder delimitedData = new StringBuilder();
            int columnCount = source.Columns.Count;
            int rowCount = source.Rows.Count;
            for (int row = 0; row < rowCount; row++)
            {
                for (int column = 0; column < columnCount; column++)
                {
                    string processedValue = "";
                    object rawValue = source.Rows[row][column];
                    if (rawValue != null)
                    {
                        processedValue = rawValue.ToString().Trim();
                        if (source.Columns[column].DataType == typeof(string))
                        {
                            processedValue = string.Format("{0}{1}{2}", textFieldPrefix, processedValue, textFieldPostfix);
                        }
                    }
                    delimitedData.Append(processedValue);
                    if (column < columnCount - 1)
                        delimitedData.Append(delimiter);
                }
                if (row < rowCount - 1)
                    delimitedData.AppendLine();
            }
            return delimitedData.ToString();
        }
    }
}
